#include "T9manager.h"


//These are CallBack routines associated to the keypad Buttons

void on_button1_clicked(GtkButton *button, Data *d)
{
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"1");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
		double elapsedtime=ElapsedTimer(d->begin);
		if((elapsedtime < 2) && (d->lastClickedButton==1)){
			d->lastClickedButton=1;
			char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
			strcpy(d->tempchar,str);
			if(d->tempchar[strlen(d->tempchar)-1]==','){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
				strcat(d->tempchar,";");
			}
			else if(d->tempchar[strlen(d->tempchar)-1]==';'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,".");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='.'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,":");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]==':'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"!");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='!'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"?");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='?'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"%");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='%'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"&");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='&'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"$");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='$'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"@");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='@'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"#");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='#'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"^");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='^'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"\\");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='\\'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"'");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='\''){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"\"");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='\"'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"_");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='_'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,",");
			}
			gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
			d->begin=BeginTimer();					
		}

		else{
			d->lastClickedButton=1;
			char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
			strcpy(d->tempchar,str);		
			strcat(d->tempchar,",");
			gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
			d->begin=BeginTimer();
		}
	}
}

void on_button2_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *caps = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCaps"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"2");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))&& !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps)))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==2)){
				d->lastClickedButton=2;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='a'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"b");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='b'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"c");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='c'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"à");
				}
		else if (d->tempchar[strlen(d->tempchar)-1]!='a'&& (d->tempchar[strlen(d->tempchar)-1]!='b') && (d->tempchar[strlen(d->tempchar)-1]!='c')){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-3);	
					strcat(d->tempchar,"a");
				}

				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=2;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"a");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}

		else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps))){

			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==2)){
				d->lastClickedButton=2;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='A'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"B");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='B'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"C");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='C'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"A");
				}			
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=2;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"A");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	

		}
		else{	
			const gchar* stringa=findWord('2',d);	
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);	
		}	
	}				
}

void on_button3_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *caps = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCaps"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"3");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}

	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))&& !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps)))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==3)){
				d->lastClickedButton=3;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='d'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"e");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='e'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"f");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='f'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"è");
				}
			else if (d->tempchar[strlen(d->tempchar)-1]!='d'&& (d->tempchar[strlen(d->tempchar)-1]!='e') && (d->tempchar[strlen(d->tempchar)-1]!='f')){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-3);	
					strcat(d->tempchar,"d");
				}


				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=3;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"d");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}

		else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps))){

			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==3)){
				d->lastClickedButton=3;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='D'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"E");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='E'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"F");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='F'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"D");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=3;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"D");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	

		}
		else{
			const gchar* stringa=findWord('3',d);	
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);
			
		}
	}
}

void on_button4_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *caps = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCaps"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"4");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))&& !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps)))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==4)){
				d->lastClickedButton=4;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='g'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"h");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='h'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"i");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='i'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"ì");
				}
			else if (d->tempchar[strlen(d->tempchar)-1]!='g'&& (d->tempchar[strlen(d->tempchar)-1]!='h') && (d->tempchar[strlen(d->tempchar)-1]!='i')){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-3);	
					strcat(d->tempchar,"g");
				}

				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=4;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"g");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}

		else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps))){

			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==4)){
				d->lastClickedButton=4;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='G'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"H");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='H'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"I");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='I'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"G");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=4;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"G");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	

		}
		else{
			const gchar* stringa=findWord('4',d);	
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);
			
		}
	}
}

void on_button5_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *caps = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCaps"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"5");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))&& !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps)))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==5)){
				d->lastClickedButton=5;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='j'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"k");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='k'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"l");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='l'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"j");
				}
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=5;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"j");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}

		else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps))){

			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==5)){
				d->lastClickedButton=5;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='J'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"K");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='K'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"L");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='L'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"J");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=5;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"J");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	

		}
		else{
			const gchar* stringa=findWord('5',d);	
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);
		}	
	
	}
}

void on_button6_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *caps = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCaps"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"6");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))&& !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps)))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==6)){
				d->lastClickedButton=6;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='m'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"n");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='n'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"o");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='o'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"ò");
				}
			else if (d->tempchar[strlen(d->tempchar)-1]!='m'&& (d->tempchar[strlen(d->tempchar)-1]!='n') && (d->tempchar[strlen(d->tempchar)-1]!='o')){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-3);	
					strcat(d->tempchar,"m");
				}

				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=6;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"m");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}

		else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps))){

			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==6)){
				d->lastClickedButton=6;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='M'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"N");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='N'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"O");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='O'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"M");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=6;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"M");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	

		}
		else{
			const gchar* stringa=findWord('6',d);	
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);
			
		}
	}
}


void on_button7_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *caps = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCaps"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"7");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))&& !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps)))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==7)){
				d->lastClickedButton=7;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='p'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"q");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='q'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"r");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='r'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"s");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='s'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"p");
				}
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=7;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"p");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}

		else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps))){

			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==7)){
				d->lastClickedButton=7;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='P'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"Q");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='Q'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"R");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='R'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"S");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='S'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"P");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=7;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"P");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	

		}
		else{
			const gchar* stringa=findWord('7',d);	
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);
			
		}
	}
}

void on_button8_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *caps = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCaps"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"8");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))&& !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps)))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==8)){
				d->lastClickedButton=8;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='t'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"u");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='u'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"v");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='v'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"ù");
				}
			else if (d->tempchar[strlen(d->tempchar)-1]!='t'&& (d->tempchar[strlen(d->tempchar)-1]!='v') && (d->tempchar[strlen(d->tempchar)-1]!='u')){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-3);	
					strcat(d->tempchar,"t");
				}

				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=8;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"t");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}

		else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps))){

			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==8)){
				d->lastClickedButton=8;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='T'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"U");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='U'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"V");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='V'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"T");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=8;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"T");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	

		}
		else{
			const gchar* stringa=findWord('8',d);	
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);
			
		}
	}

}

void on_button9_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *caps = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCaps"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"9");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))&& !(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps)))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==9)){
				d->lastClickedButton=9;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='w'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"x");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='x'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"y");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='y'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"z");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='z'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"w");
				}
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=9;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"w");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}

		else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(caps))){

			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==9)){
				d->lastClickedButton=9;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='W'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"X");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='X'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"Y");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='Y'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"Z");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='Z'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"W");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=9;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"W");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	

		}
		else{
			const gchar* stringa=findWord('9',d);	
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);
			
		}
	}	
}

void on_button0_clicked(GtkButton *button, Data *d){
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"0");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	
}


void on_buttoncancell_clicked(GtkButton *button, Data *d)//Switches words only in T9 mode
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(!(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num)))){
		const gchar* stringa=findNextWord(d->currentword,d->currentnode->paroleHead,d);
		gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);	
	}
}

void on_buttonasterix_clicked(GtkButton *button, Data *d)
{
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		double elapsedtime=ElapsedTimer(d->begin);
		if((elapsedtime < 2) && (d->lastClickedButton==10)){
			d->lastClickedButton=10;
			char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
			strcpy(d->tempchar,str);
			if(d->tempchar[strlen(d->tempchar)-1]=='*'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
				strcat(d->tempchar,"+");
			}
			else if(d->tempchar[strlen(d->tempchar)-1]=='+'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"-");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='-'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"/");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='/'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"=");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='='){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"(");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='('){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,")");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]==')'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"[");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='['){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"]");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]==']'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"<");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='<'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,">");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='>'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"*");
			}
			gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
			d->begin=BeginTimer();					
		}

		else{
			d->lastClickedButton=10;
			char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
			strcpy(d->tempchar,str);		
			strcat(d->tempchar,"*");
			gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
			d->begin=BeginTimer();
		}
	}
}


void on_buttonClear_clicked(GtkButton *button, Data *d)
{
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkEditable *entry = (GtkEditable*)d->textarea;	
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	gint* position;	
	char* str=NULL;	
	str= gtk_editable_get_chars(entry,0,-1);
	
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		if (strlen(str)==0){
			sendChar(0xff08,0);
			
		}

		else{
			char* strMinusLastChar=NULL;
			strMinusLastChar=(char*)malloc(wordlen*sizeof(char));			
			strcpy(strMinusLastChar,str);	
			strMinusLastChar=substring_r(strMinusLastChar, strMinusLastChar, 0, strlen(strMinusLastChar)-2);				
			gtk_entry_set_text(GTK_ENTRY(entry),strMinusLastChar);			
		}	

	}
	else{
		if (strlen(str)==0){
			sendChar(0xff08,0);
			
		}

		else if(strlen(str)==1||strcmp(str,"è")==0||strcmp(str,"à")==0||strcmp(str,"ì")==0||strcmp(str,"ò")==0||strcmp(str,"ù")==0) {
			gtk_editable_delete_text(entry,0,-1);	
			char* stringa=findPrevWord(d);			
		}
		else{
			const gchar* stringa=findPrevWord(d);
			gtk_entry_set_text(GTK_ENTRY(entry),stringa);	
		}
	}
}

void on_buttonSpace_clicked(GtkButton *button, Data *d){
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkEditable *entry = (GtkEditable*)d->textarea;	
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	gint* position;	
	char* str=NULL;	
	str= gtk_editable_get_chars(entry,0,-1);
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
		if(tobeSaved(str))
			d->listat9=inserisciElemento(&(d->listat9),getCodeFromString(str),str,1);
	}
	
	else{
		d->charnumber=0;
		if(d->currentword!=NULL)
			d->currentword->count++;
	}
		
		gtk_editable_delete_text(entry,0,-1);		
		int i=0;
		sendString(str);
		sendChar(' ',0);		
		d->currentnode=d->listat9;
		d->tempstring[0]='\0';
		d->tempnum[0]='\0';
		d->currentword=NULL;
		d->tempchar[0]='\0';
		d->lastClickedButton=-1;




}
void on_buttonOK_clicked(GtkButton *button, Data *d)
{
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkEditable *entry = (GtkEditable*)d->textarea;	
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	gint* position;	
	char* str=NULL;	
	str= gtk_editable_get_chars(entry,0,-1);
	if(strlen(str)==0){
		sendChar(0xff0d,0);
	}
	else{
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
			if(tobeSaved(str))
				d->listat9=inserisciElemento(&(d->listat9),getCodeFromString(str),str,1);
		}
	
		else{
			d->charnumber=0;
			if(d->currentword!=NULL)
				d->currentword->count++;
		}
		gtk_editable_delete_text(entry,0,-1);		
		int i=0;
		sendString(str);
		d->currentnode=d->listat9;
		d->tempstring[0]='\0';
		d->tempnum[0]='\0';
		d->currentword=NULL;
		d->tempchar[0]='\0';
		d->lastClickedButton=-1;
	}

}



void on_togglebuttonNew_toggled(GtkToggleButton *button, Data *d){

	GtkEditable *entry = (GtkEditable*)d->textarea;
	gtk_editable_delete_text(entry,0,-1);	
	d->currentnode=d->listat9;
	d->tempstring[0]='\0';
	d->tempnum[0]='\0';
	d->currentword=NULL;
	d->tempchar[0]='\0';
	d->lastClickedButton=-1;


}

void on_togglebuttonCaps_toggled(GtkToggleButton *button, Data *d){
	
	GtkEditable *entry = (GtkEditable*)d->textarea;
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	gint* position;	
	char* str=NULL;	
	str= gtk_editable_get_chars(entry,0,-1);
	if(!(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)))){
		if(!containsAccentedChar(str)){
			if(isUpperString(str))
				gtk_entry_set_text(GTK_ENTRY(d->textarea),toLowerString((char*)str));
			else 
				gtk_entry_set_text(GTK_ENTRY(d->textarea),toUpperString((char*)str));
		}
		else{
			gtk_entry_set_text(GTK_ENTRY(d->textarea),invertAccentedStrings((char*)str));
		}
	}
		
}

void on_togglebuttonNum_toggled(GtkToggleButton *button, Data *d){}

void on_togglebuttonLock_toggled(GtkToggleButton *button, Data *d)
{
	if(gtk_entry_get_visibility((GtkEntry*)d->textarea))	
		gtk_entry_set_visibility((GtkEntry*)d->textarea,FALSE);
	else
		gtk_entry_set_visibility((GtkEntry*)d->textarea,TRUE);
}

void on_togglebuttonShift_toggled(GtkToggleButton *button, Data *d)
{
	GtkWidget *shift = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonShift"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(shift)))
		system("xdotool keydown shift");
	else
		system("xdotool keyup shift");

}

void on_buttonAlt_clicked(GtkButton *button, Data *d)
{
	GtkWidget *alt = GTK_WIDGET(gtk_builder_get_object(d->builder, "buttonAlt"));
	system("xdotool keydown alt");
	sleep(5);
	system("xdotool keyup alt");
	
}

void on_togglebuttonCtrl_toggled(GtkToggleButton *button, Data *d)
{
	GtkWidget *ctrl = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCtrl"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ctrl)))
		system("xdotool keydown ctrl");
	else
		system("xdotool keyup ctrl");

}


void on_togglebuttonIta_toggled(GtkToggleButton *button, Data *data)
{
	GtkWidget *eng = GTK_WIDGET(gtk_builder_get_object(data->builder, "togglebuttonEng"));
	GtkWidget *ita = GTK_WIDGET(gtk_builder_get_object(data->builder, "togglebuttonIta"));
	GtkEditable *entry = (GtkEditable*)data->textarea;	

	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ita))){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(eng),FALSE);
		gtk_editable_delete_text(entry,0,-1);		
		data->listat9=NULL;
		data->tempstring[0]='\0';
		data->tempnum[0]='\0';
		data->currentword=NULL;
		data->tempchar[0]='\0';
		data->charnumber=0;
		data->lastClickedButton=-1; 
		strcpy(data->dictionary,data->dictionaryIta);
		//data->listat9=initDictionary(data->dictionary,data);	
		data->listat9=data->listat9ita;
		data->currentnode=data->listat9;
	}

	else{
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ita),FALSE);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(eng),TRUE);
		gtk_editable_delete_text(entry,0,-1);		
		data->listat9=NULL;
		data->tempstring[0]='\0';
		data->tempnum[0]='\0';
		data->currentword=NULL;
		data->tempchar[0]='\0';
		data->charnumber=0;
		data->lastClickedButton=-1; 
		strcpy(data->dictionary,data->dictionaryEng);
		//data->listat9=initDictionary(data->dictionary,data);
		data->listat9=data->listat9eng;	
		data->currentnode=data->listat9;
	}	
	
		
}


void on_togglebuttonEng_toggled(GtkToggleButton *button, Data *data)
{
	GtkWidget *ita = GTK_WIDGET(gtk_builder_get_object(data->builder, "togglebuttonIta"));
	GtkWidget *eng = GTK_WIDGET(gtk_builder_get_object(data->builder, "togglebuttonEng"));
	GtkEditable *entry = (GtkEditable*)data->textarea;	
	
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(eng))){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ita),FALSE);
		gtk_editable_delete_text(entry,0,-1);		
		data->listat9=NULL;
		data->tempstring[0]='\0';
		data->tempnum[0]='\0';
		data->currentword=NULL;
		data->tempchar[0]='\0';
		data->charnumber=0;
		data->lastClickedButton=-1; 
		strcpy(data->dictionary,data->dictionaryEng);
		data->listat9=data->listat9eng;
		data->currentnode=data->listat9;
	}
	else{
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(eng),FALSE);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ita),TRUE);
		gtk_editable_delete_text(entry,0,-1);		
		data->listat9=NULL;
		data->tempstring[0]='\0';
		data->tempnum[0]='\0';
		data->currentword=NULL;
		data->tempchar[0]='\0';
		data->charnumber=0;
		data->lastClickedButton=-1; 
		strcpy(data->dictionary,data->dictionaryIta);
		data->listat9=data->listat9ita;
		data->currentnode=data->listat9;
	}
	
		
}


GtkWidget *get_object_from_builder(GtkBuilder *builder, gchar *obj_id){

	if(builder && obj_id){	
		GtkWidget *widget = GTK_WIDGET(gtk_builder_get_object(builder, obj_id));
		if(widget)return widget;
		else{
			g_printf("Object ID: %s not found.\n", obj_id);
			return NULL;
		}
	}
}


//This routine connect all the signals to the buttons
void add_all_events(Data *d){

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "window1")), "delete_event",
		G_CALLBACK(gtk_main_quit), NULL);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button1")), "clicked",
		G_CALLBACK(on_button1_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button2")), "clicked",
		G_CALLBACK(on_button2_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button3")), "clicked",
		G_CALLBACK(on_button3_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button4")), "clicked",
		G_CALLBACK(on_button4_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button5")), "clicked",
		G_CALLBACK(on_button5_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button6")), "clicked",
		G_CALLBACK(on_button6_clicked), d);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button7")), "clicked",
		G_CALLBACK(on_button7_clicked), d);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button8")), "clicked",
		G_CALLBACK(on_button8_clicked), d);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button9")), "clicked",
		G_CALLBACK(on_button9_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button0")), "clicked",
		G_CALLBACK(on_button0_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttoncancell")), "clicked",
		G_CALLBACK(on_buttoncancell_clicked), d);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttonasterix")), "clicked",
		G_CALLBACK(on_buttonasterix_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttonClear")), "clicked",
		G_CALLBACK(on_buttonClear_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttonOK")), "clicked",
		G_CALLBACK(on_buttonOK_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttonSpace")), "clicked",
		G_CALLBACK(on_buttonSpace_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonNew")), "toggled",
		G_CALLBACK(on_togglebuttonNew_toggled), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonCaps")), "toggled",
			G_CALLBACK(on_togglebuttonCaps_toggled), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonNum")), "toggled",
			G_CALLBACK(on_togglebuttonNum_toggled), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonLock")), "toggled",
			G_CALLBACK(on_togglebuttonLock_toggled), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonCtrl")), "toggled",
			G_CALLBACK(on_togglebuttonCtrl_toggled), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttonAlt")), "clicked",
			G_CALLBACK(on_buttonAlt_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonShift")), "toggled",
			G_CALLBACK(on_togglebuttonShift_toggled), d);

	g_signal_connect(GTK_STATUS_ICON(d->trayIcon), "activate", GTK_SIGNAL_FUNC (trayIconActivated), d->the_window1);
    	
	g_signal_connect (G_OBJECT (d->the_window1), "destroy", G_CALLBACK(destroy), d);
    	
	g_signal_connect (G_OBJECT (d->the_window1), "delete_event", G_CALLBACK (delete_event), d->trayIcon);
   	
	g_signal_connect (G_OBJECT (d->the_window1), "window-state-event", G_CALLBACK (window_state_event), d->trayIcon);
	
	g_signal_connect (G_OBJECT(get_object_from_builder(d->builder, "togglebuttonIta")), "toggled", G_CALLBACK(on_togglebuttonIta_toggled), d);

	g_signal_connect (G_OBJECT(get_object_from_builder(d->builder, "togglebuttonEng")), "toggled", G_CALLBACK(on_togglebuttonEng_toggled), d);
	
	
	}
//These routines are associated to button's signal to manage minimizing the keypad and to put an icon on tray bar

static void trayIconActivated(GObject *trayIcon, gpointer window)
{
    gtk_widget_set_can_focus(GTK_WIDGET(window),FALSE);
    gtk_widget_show(GTK_WIDGET(window));
    gtk_window_deiconify(GTK_WINDOW(window));
    gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), FALSE);
}


static void destroy (GtkWidget *window, Data* data)
{
	saveDictionary(data->listat9,data->dictionary);
	free(data->listat9ita);
	free(data->listat9eng);
	gtk_main_quit ();
}

static gboolean delete_event (GtkWidget *window, GdkEvent *event, gpointer data)
{
    return FALSE;
}

static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer trayIcon)
{
    if(event->changed_mask == GDK_WINDOW_STATE_ICONIFIED && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_ABOVE)))
    {
	gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), TRUE);
	g_idle_add((GSourceFunc)gtk_widget_hide,widget);
	gtk_widget_set_can_focus(widget,TRUE);
	gtk_widget_grab_focus(widget);
	Display *display;
	int screen;
	Window window;
	display = XOpenDisplay(NULL);
	if(display==NULL)
		printf("\nCannot open display");
	screen= DefaultScreen(display);
	int revert_to; 
	XGetInputFocus(display, &window, &revert_to);
	XClearWindow(display,window);
	XFlush(display);
	XCloseDisplay(display);
	
    }
    else if(event->changed_mask == GDK_WINDOW_STATE_WITHDRAWN && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_ABOVE)))
    {
        gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), FALSE);
    }
    return TRUE;
}




